//
//  SetInfoViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/4/18.
//

#import "SetInfoViewController.h"
#import "SetUpTableViewCell.h"
#import "SetUpTopView.h"
#import "InfoModel.h"
#import "Info1ViewController.h"
#import <BRPickerView/BRPickerView.h>
#import "Info2ViewController.h"
#import "Info4ViewController.h"
#import "InfoMapViewController.h"
#import "NameEditView.h"
#import "HeightWeightView.h"
#import "PhotoViewController.h"
#import "AboutMeViewController.h"
#import "MeBgImageViewController.h"
@interface SetInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) SetUpTopView * topView;
@end

@implementation SetInfoViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self SQ_Header];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"完善个人资料");
    // Do any additional setup after loading the view.
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bg = [[UIImageView alloc]init];
    bg.image = SQGetImage(@"me背景");
    bg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.tableHeaderView = self.topView;
    self.tableView.delegate  = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    [self SQ_tableViewAddRefresh];
  
}
-(void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [HttpTool post:Url6 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [MeModel SQ_setuserModel:httpModel];
        MeModel * model = [MeModel mj_objectWithKeyValues:httpModel.data];
        [InfoModel SQ_getArray:self.jsonArray model:model];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)SQ_Foother{
    [self.tableView.mj_footer endRefreshing];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SetUpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SetUpTableViewCell"];
    if (!cell) {
        cell = [[SetUpTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"SetUpTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model =  [self.jsonArray objectAtIndex:indexPath.item];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  66;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    InfoModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    if ([model.leftString isEqualToString:@"昵称"]) {
        [self SQ_popNameView];
    } else if([model.leftString isEqualToString:@"生日"]) {
        [self SQ_birthdayButtonClick:model.rightString];
    }else if([model.leftString isEqualToString:@"星座"]) {
        Info2ViewController * vc = [[Info2ViewController alloc]init];
        vc.isEdit = YES;
        [self SQ_pushViewControllerFull:vc];
    }else if([model.leftString isEqualToString:@"身高"]) {
        [self SQ_popHeightWeightView:YES rightValue:model.rightString];
    }else if([model.leftString isEqualToString:@"体重"]) {
        [self SQ_popHeightWeightView:NO rightValue:model.rightString];
    }else if([model.leftString isEqualToString:@"兴趣"]) {
        Info4ViewController * vc = [[Info4ViewController alloc]init];
        vc.isEdit = YES;
        vc.rigntString = model.rightString;
        [self SQ_pushViewControllerFull:vc];
    }else if([model.leftString isEqualToString:@"常住地"]) {
        InfoMapViewController * vc = [[InfoMapViewController alloc]init];
        vc.isEdit = YES;
        [self SQ_pushViewControllerFull:vc];
    }else if([model.leftString isEqualToString:@"性别"]) {
        if ([model.rightString isNotEmpty]) {
            return;
        }
        Info1ViewController * vc = [[Info1ViewController alloc]init];
        vc.isEdit = YES;
        [self SQ_pushViewControllerFull:vc];
    }else if([model.leftString isEqualToString:@"相册"]) {
        PhotoViewController *vc = [[PhotoViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else if([model.leftString isEqualToString:@"关于我"]) {
        AboutMeViewController *vc = [[AboutMeViewController alloc]init];
        vc.aboutString = model.rightString;
        [self SQ_pushViewControllerFull:vc];
    }else if([model.leftString isEqualToString:@"个人背景"]) {
        MeBgImageViewController * vc = [[MeBgImageViewController alloc]init];
        vc.aboutString = model.rightString;
        [self SQ_pushViewControllerFull:vc];
    }
    
}


-(void)SQ_birthdayButtonClick:(NSString*)birthday{
    WS(weakSelf);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString* selectValue = [NSString SQ_GetoutYear:-30];
    if ([birthday isNotEmpty]) {
        selectValue = birthday;
    }
    NSString* minDate = [NSString SQ_GetoutYear:-100];
    NSString* maxDate = [NSString SQ_GetoutYear:-18];
    [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:@"" selectValue:selectValue minDate:[dateFormatter dateFromString:minDate] maxDate:[dateFormatter dateFromString:maxDate] isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        [weakSelf SQ_Birthday:selectValue];
    } resultRangeBlock:^(NSDate * _Nullable selectStartDate, NSDate * _Nullable selectEndDate, NSString * _Nullable selectValue) {
        
    }];
}
-(void)SQ_Birthday:(NSString*)selectValue{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:selectValue forKey:@"birthday"];
    [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_Header];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_popNameView{
    NameEditView * vc = [[NameEditView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 260+FootHeight)];
    
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc setIsNameBtnBlock:^{
        [weakSelf SQ_Header];
        [wk_pop dismiss];
    }];
}
-(void)SQ_popHeightWeightView:(BOOL)isHeight rightValue:(NSString*)value{
    HeightWeightView * vc = [[HeightWeightView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 260+FootHeight)];
    [vc SQ_setLines:isHeight right:value];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc setIsheightOrWeightBtnBlock:^{
        [weakSelf SQ_Header];
        [wk_pop dismiss];
    }];
}


- (SetUpTopView *)topView{
    if (!_topView) {
        _topView = [[SetUpTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 150)];
    }
    return _topView;
}

@end
